package com.hustzp.com.xichuangzhu.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.Constant;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends XCZBaseFragmentActivity {
    private String TAG = "LoginActivity";

    private EditText mPasswordView, tPhoneNumber;
    private TextView tMiddle, tRight, tLeft;
    private ImageView tLeftImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    public void initView() {
        tPhoneNumber = (EditText) findViewById(R.id.phone_number);
        mPasswordView = (EditText) findViewById(R.id.password);
        tMiddle = (TextView) findViewById(R.id.middle_text);
        tRight = (TextView) findViewById(R.id.right_text);
        tLeftImage = (ImageView) findViewById(R.id.left_Image);
        tMiddle.setText(getResources().getString(R.string.login_));
        tRight.setText(getResources().getString(R.string.register));
        tLeftImage.setOnClickListener(viewOnclick);
        tRight.setOnClickListener(viewOnclick);
        findViewById(R.id.login_button).setOnClickListener(viewOnclick);
        findViewById(R.id.forget_password).setOnClickListener(viewOnclick);
    }

    public OnClickListener viewOnclick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int viewID = v.getId();
            switch (viewID) {
                case R.id.left_Image:
                    LoginActivity.this.finish();
                    break;
                case R.id.right_text:
                    Intent intent1 = new Intent(LoginActivity.this, ReceiveSmsCodeActivity.class);
                    intent1.setAction(Constant.INTENT_ACTION_REGISTER);
                    startActivity(intent1);
                    break;
                case R.id.forget_password:
                    Intent intent2 = new Intent(LoginActivity.this, ReceiveSmsCodeActivity.class);
                    intent2.setAction(Constant.INTENT_ACTION_RESTPASW);
                    startActivity(intent2);
                    break;
                case R.id.login_button:
                    login();
                    break;
            }
        }
    };

    public void login() {
        if (TextUtils.isEmpty(tPhoneNumber.getText().toString().trim())) {
            DialogFactory.hintDialog(LoginActivity.this, getResources().getString(R.string.phone_number_not_empity));
            return;
        }
        if (TextUtils.isEmpty(mPasswordView.getText().toString().trim())) {
            DialogFactory.hintDialog(LoginActivity.this, getResources().getString(R.string.password_not_empity));
            return;
        }

        AVUser.loginByMobilePhoneNumberInBackground(tPhoneNumber.getText().toString().trim(), mPasswordView.getText().toString().trim(),
                new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (avUser != null) {
                    finish();
                } else {
                    String message = e.getMessage();
                    try {
                        JSONObject messageObject = new JSONObject(message);
                        String code = messageObject.getString("code");
                        if("215".equals(code)){

                            DialogFactory.hintDialog(LoginActivity.this,"未验证的手机号码");
                            // 找不到用户
                        }else if("211".equals(code)){
                            DialogFactory.hintDialog(LoginActivity.this,"找不到用户");
                        }else {
                            DialogFactory.hintDialog(LoginActivity.this,e.getMessage());
                        }

                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
//                    DialogFactory.hintDialog(LoginActivity.this, getResources().getString(R.string.phonenumber_not_match_passwd));

                }
            }
        });
    }
}

